package com.api.gbhg.lbj.api;

import com.alibaba.fastjson.JSON;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.lbj.tool.Tools.getsfkzsydwbxed;
import static com.api.gbhg.lbj.tool.Tools.getsubcompanyId;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 校验部门支出分类经费额度(明细表)
 * @date 2021/6/10 14:57
 */
@Path("/gbhg/funds")
public class FundsquotaCheckApi {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @POST
    @Path("/checkFunds")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String fundsCheck(Map<String, String>[] param){
        logger.info("进入部门支出分类经费额度校验--fundsCheck");
        Map<String,Object> map=new HashMap<>();
        for (int i=0;i<param.length;i++) {
            BigDecimal sqbxje = new BigDecimal(param[i].get("sqbxje"));//申请报销金额
            BigDecimal tybxje = new BigDecimal(param[i].get("tybxje"));//同意报销金额
            String lcjlid = param[i].get("ysxm");//预算项目
            String bmjjzcfl = param[i].get("bmjjzcfl");//部门经济支出分类
            String sfkj = param[i].get("sfkj");//是否会计节点 1:是 0:否
            RecordSet rs=new RecordSet();
            String sql="select syed from uf_jingfei where lcjlid="+lcjlid+" and bmzcjjfl="+bmjjzcfl;
            logger.info("部门支出分类剩余额度sql:"+sql);
            rs.executeQuery(sql);
            while (rs.next()){
                BigDecimal syed = new BigDecimal(rs.getString("syed"));
                logger.info("申请报销金额:"+sqbxje+",同意报销金额:"+tybxje+",剩余额度:"+syed);
                if (sfkj.equals("1")){
                    syed=syed.add(sqbxje);
                    if (tybxje.compareTo(syed)==1){
                        map.put("code", "300");
                        map.put("msg", ",部门支出分类经费额度不足！");
                    }else {
                        map.put("code","200");
                    }
                }else {
                    if (sqbxje.compareTo(syed)==1){
                        map.put("code", "300");
                        map.put("msg", ",部门支出分类经费额度不足！");
                    }else {
                        map.put("code","200");
                    }
                }
            }
        }
        logger.info("JSON.toJSONString(map):"+JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }
}
